package com.ilovepostcard;

import java.util.ArrayList;

import com.ilovepostcard.controller.PostcardController;
import com.ilovepostcard.controller.ShareController;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.IServer;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.data.Postcard;
import com.ilovepostcard.data.SendTaskInfo;
import com.ilovepostcard.model.Address;
import com.ilovepostcard.view.MyEditText;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class PrintConfirmCommentActivity extends BaseActivity implements OnClickListener{

	ImageView mReturn;
	Button mConfirm;
	RadioButton comment;
	RadioButton noComment;
	MyEditText mInput;
	//TextView mCounter;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_print_confirmcomment);
		init();
	}
	
	private void init()
	{
		mReturn = (ImageView)findViewById(R.id.title_return);
		mReturn.setOnClickListener(this);
		
		mConfirm = (Button)findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		
		comment = (RadioButton)findViewById(R.id.useComment);
		
		String text = this.getResources().getString(R.string.add_address_confirm_comment_tip);
		final SpannableStringBuilder sb = new SpannableStringBuilder(text);  
	    final ForegroundColorSpan fcs = new ForegroundColorSpan(this.getResources().getColor(R.color.color_gray_1));
		sb.setSpan(fcs, 6, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // Set the text color for first 4 characters  
		comment.setText(sb);  
		Postcard cPc = PostcardController.getInstance().getCurrentPostCard();
		mInput =  (MyEditText)findViewById(R.id.comment_input);
		mInput.setText(cPc.comment);
		/*mCounter = (TextView)findViewById(R.id.Counter);
		initEditText(sb.toString());
		mInput.addTextChangedListener(new TextWatcher() {   
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {   

            	initEditText(s== null ? null : s.toString());
				
            }   
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,   
                    int after) {   

            }   
            @Override  
            public void afterTextChanged(Editable s) {   
            	
            
            }   
        });  */
		
		
		if(cPc.comment!= null)
			mInput.setSelection(cPc.comment.length());
		noComment = (RadioButton)findViewById(R.id.noComment);
		
	}
	
	private void initEditText(String s)
	{
		int total = s == null ? 0 : s.length();
    	String message = getResources().getString(R.string.edit_counter);
		String formatMSG = String.format(message, total);
		//mCounter.setText(formatMSG);

		//mCounter.setTextColor(total>=140 ? Color.RED : getResources().getColor(R.color.color_gray_2));
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.title_return)
		{
			finish();
		}
		else if(v.getId() == R.id.confirm)
		{


			SendTaskInfo sendtask =  ShareController.getInstance().getCurrentSendTask();
			if(comment.isChecked())
			{
				sendtask.hasComment = true;
				if(mInput.getEditableText()!= null)
					sendtask.comment = mInput.getEditableText().toString();
				else
					sendtask.comment =  null;
			}
			else if(noComment.isChecked())
			{
				sendtask.hasComment = false;
				sendtask.comment =  null;
			}
			else
			{
				Toast.makeText(this, "请选择寄语类型", Toast.LENGTH_SHORT).show();
				return;
			}
			
			IServer server = ServerFactory.getServer();
			PostcardResult res = server.listAddress(UserController.getLoginUser(this).UserID, 1, 10);
			ArrayList<Address> address = res.getAddressList();
			
			if(address == null || address.size() == 0)
			{
				Intent nextPage = new Intent(this,PrintAddressActivity.class);
				startActivity(nextPage);
			}
			else
			{
				Intent nextPage = new Intent(this,PrintChooseAddressActivity.class);
				startActivity(nextPage);
			}
			
			
		}
		
	}
	
}
